package com.example.mysaat.service_menu_manual_test.AllFunctions;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SDCard {
    private Activity activity = null;
    private StorageVolume[] volumes = null;
    private StorageManager storageManager = null;
    private Boolean isPass = Boolean.FALSE;
    private Boolean isRegister = false;

    private static final String TAG = "SDCard";
    public SDCard (Activity activity){
        this.activity = activity;
        storageManager = (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE);
    }

    public Boolean isExistSdCardPass(){
        try {
            Method method = StorageManager.class.getDeclaredMethod("getVolumeList");
            method.setAccessible(true);
            volumes = (StorageVolume[])method.invoke(storageManager);
            if(volumes!=null&&isMounted(volumes)){
                isPass = Boolean.TRUE;
            }else{
                IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
                filter.addDataScheme("file");
                activity.registerReceiver(broadcast,filter);
                isRegister = true;
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "isExistSdCardPass: SD test finished");

        return isPass;
    }

    private final BroadcastReceiver broadcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent !=null){
                String action  = intent.getAction();
                if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
                    try {
                        Method method = StorageManager.class.getDeclaredMethod("getVolumeList");
                        method.setAccessible(true);
                        volumes = (StorageVolume[])method.invoke(storageManager);
                        if (isMounted(volumes)){
                            isPass = Boolean.TRUE;
                        }else{
                            isPass = Boolean.FALSE;
                            Log.d(TAG, "isExistSdCardPass: SD test fail by broadcast");
                        }
                        activity.unregisterReceiver(broadcast);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean isMounted(StorageVolume[] volumes){
        if(volumes==null)
            return false;

        for (StorageVolume volume : volumes){
            if(volume.isRemovable()){
                try {
                    Method myVolumeState = StorageManager.class.getDeclaredMethod("getVolumeState",String.class);
                    myVolumeState.setAccessible(true);
                    Method myPath = StorageVolume.class.getDeclaredMethod("getPath");
                    myPath.setAccessible(true);
                   final String state = (String) myVolumeState.invoke(storageManager,(String)myPath.invoke(volume));
                   if (Environment.MEDIA_MOUNTED.equals(state))
                       return true;
                   else
                       return false;
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }else
                continue;
        }
        return  false;
    }

    public boolean isRegister(){
        return  this.isRegister;
    }

    public void mUnregisterReceiver(){
        activity.unregisterReceiver(broadcast);
    }
}
